How to convert System.Object that's really an int32[] to a double[] ?
Posted
by fs_tech
on Stack Overflow
See other posts from Stack Overflow
or by fs_tech
Published on 2010-03-10T21:13:16Z
Indexed on
2010/03/11
21:34 UTC
Read the original article
Hit count: 116
F#
Hello-
I get data from a 3rd party API that just gives me back a System.Object, which I know to be a double[] under the covers. And to deal with that return type, I have found the code below to work wonderfully. However, I also get back some int[] arrays that are also masquerading as System.Object, specifically dates in the form YYYYMMDD (e.g. 20100310).
The conversion to float fails, and it just says that the specified cast is not valid. Does anyone out there know how to make this work for integers?
let oIsNull (obj : System.Object) = obj = null
let oIsArray (obj : System.Object) = obj.GetType().IsArray
let o2f (obj : System.Object) =
let mutable arr = [|Double.NaN|]
if (oIsNull obj = false) && (oIsArray obj = true) then
let objArr = obj :?> obj[]
let u = objArr.GetUpperBound(0)
let floatArr : float[] = Array.zeroCreate (u + 1);
for i in 0..u do
if objArr.[i] = null then
floatArr.[i] <- Double.NaN
else
let t = objArr.[i].GetType()
floatArr.[i] <- objArr.[i] :?> float
//else floatArr.[i] <- float objArr.[i]
arr <- floatArr
arr
© Stack Overflow or respective owner